/**
 * Analytics 模块 API
 * 分析统计相关接口
 */

import { request, type ApiResponse, type RequestParamValue } from "../request";

// ============ 类型定义 ============

/**
 * 时间间隔
 */
export type AnalyticsInterval = "15min" | "30min" | "1hr" | "3hr" | "1day" | "3day" | "7day";

/**
 * 函数统计时间间隔
 */
export type FunctionStatsInterval = "15min" | "1hr" | "3hr" | "1day";

/**
 * 分析错误详情
 */
export interface AnalyticsErrorDetail {
	domain: string;
	location: string;
	locationType: string;
	message: string;
	reason: string;
}

/**
 * 分析错误对象
 */
export interface AnalyticsErrorObject {
	code: number;
	errors: AnalyticsErrorDetail[];
	message: string;
	status: string;
}

/**
 * 通用分析响应
 */
export interface AnalyticsResponse<T = unknown> {
	result?: T[];
	error?: string | AnalyticsErrorObject;
}

/**
 * API 使用统计项
 */
export interface ApiCountItem {
	timestamp: string;
	total_auth_requests: number;
	total_realtime_requests: number;
	total_rest_requests: number;
	total_storage_requests: number;
}

/**
 * API 使用统计响应
 */
export interface UsageApiCountResponse {
	result?: ApiCountItem[];
	error?: string | AnalyticsErrorObject;
}

/**
 * API 请求计数项
 */
export interface ApiRequestsCountItem {
	count: number;
}

/**
 * API 请求计数响应
 */
export interface UsageApiRequestsCountResponse {
	result?: ApiRequestsCountItem[];
	error?: string | AnalyticsErrorObject;
}

/**
 * 日志查询参数
 */
export interface GetLogsParams {
	sql?: string;
	iso_timestamp_start?: string;
	iso_timestamp_end?: string;
}

/**
 * API 使用统计查询参数
 */
export interface GetUsageApiCountParams {
	interval?: AnalyticsInterval;
}

/**
 * 函数统计查询参数
 */
export interface GetFunctionStatsParams {
	interval: FunctionStatsInterval;
	function_id: string;
}

// ============ API 方法 ============

/**
 * 获取项目日志
 * 在项目日志上执行 SQL 查询
 */
export function getProjectLogs(
	ref: string,
	params?: GetLogsParams
): Promise<ApiResponse<AnalyticsResponse>> {
	return request.get<AnalyticsResponse>(
		`/v1/projects/${ref}/analytics/endpoints/logs.all`,
		params as Record<string, RequestParamValue>
	);
}

/**
 * 获取项目 API 使用统计
 */
export function getUsageApiCounts(
	ref: string,
	params?: GetUsageApiCountParams
): Promise<ApiResponse<UsageApiCountResponse>> {
	return request.get<UsageApiCountResponse>(
		`/v1/projects/${ref}/analytics/endpoints/usage.api-counts`,
		params as Record<string, RequestParamValue>
	);
}

/**
 * 获取项目 API 请求总数
 */
export function getUsageApiRequestsCount(
	ref: string
): Promise<ApiResponse<UsageApiRequestsCountResponse>> {
	return request.get<UsageApiRequestsCountResponse>(
		`/v1/projects/${ref}/analytics/endpoints/usage.api-requests-count`
	);
}

/**
 * 获取项目函数综合统计
 */
export function getFunctionCombinedStats(
	ref: string,
	params: GetFunctionStatsParams
): Promise<ApiResponse<AnalyticsResponse>> {
	return request.get<AnalyticsResponse>(
		`/v1/projects/${ref}/analytics/endpoints/functions.combined-stats`,
		params as Record<string, RequestParamValue>
	);
}

export default {
	getProjectLogs,
	getUsageApiCounts,
	getUsageApiRequestsCount,
	getFunctionCombinedStats
};
